<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Kaopu
 *
 * @author dragon.hu add @2016.2.17
 */
require_once 'BaseChannel.php';

//靠谱
class Kaopu implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="4BACD68C-3418-4BEE-B0BF-317F0D0D7E06"; //KAOPU_SECRETKEY
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$kaopuSecret = "4BACD68C-3418-4BEE-B0BF-317F0D0D7E06";
		
		$orderLog = new ChargeOrder();
		$orderLog->log("kaopu", $_REQUEST);

		$username = $_REQUEST['username'];  //充值用户名
		$kpordernum = $_REQUEST['kpordernum'];  //靠谱订单ID
		$ywordernum = $_REQUEST['ywordernum'];  //CP订单ID
		$status = $_REQUEST['status'];  //1为成功其他失败
		$paytype = $_REQUEST['paytype'];  //充值类型
		$amount = $_REQUEST['amount'];  //单位分
		$gameserver = $_REQUEST['gameserver'];  //游戏区服
		$errdesc = $_REQUEST['errdesc'];  //充值失败错误码
		$paytime = $_REQUEST['paytime'];  //充值成功时间
		$gamename = $_REQUEST['gamename'];  //游戏名称
		$sign = $_REQUEST['sign'];  //签名
		
		//所有参数(除 sign)的 MD5签名拼串: username 值|kpordernum 值|ywordernum 值| status值| paytype 值| amount值|gameserver 值|errdesc 值| paytime 值|gamename 值|signkey值
		$mysign = $username."|".$kpordernum."|".$ywordernum."|".$status."|".$paytype."|".$amount."|".$gameserver."|".$errdesc."|".
				$paytime."|".$gamename."|".$paymentKey;
		
		if($sign == md5($mysign))  
		{ //验证通过,进行发货
			$tmpstatus = STATUS_SUCCESS;
			if (1 != $status)  //1=成功,其他失败
			{
				$tmpstatus = STATUS_FAILD;
			}
			
			if(false == $orderLog->recordOrder($tmpstatus, $amount/100, $ywordernum, "kaopu", $platform,$kpordernum))
			{
				$result['code'] = '1005';
				$result['msg'] = 'insert into db failed';
				$result['sign'] = md5($result['code'].'|'.$kaopuSecret);
				$orderLog->log("kaopu",array("order"=>$ywordernum,"money"=>$amount,"serverresult"=>"faild"));
			}
			else 
			{
				//支付成功
				$result['code'] = '1000';
				$result['msg'] = 'success';
				$result['sign'] = md5($result['code'].'|'.$kaopuSecret);
				$orderLog->log("kaopu",array("order"=>$ywordernum,"money"=>$amount,"serverresult"=>"success"));
			}
			 
		} 
		else 
		{
			$result['code'] = '1002';
			$result['msg'] = 'sign fail';
			$result['sign'] = md5($result['code'].'|'.$kaopuSecret);
			$orderLog->log("kaopu","  order:$ywordernum  sign error sign:$sign mysign:$mysign");
		}
		$ret = json_encode($result);
		return $ret;
	}
	
}